package org.light.domain;

import java.util.Iterator;
import java.util.Set;

import org.light.complexverb.AddUploadDomainField;
import org.light.complexverb.DomainFieldVerb;
import org.light.exception.ValidateException;
import org.light.generator.NamedUtilMethodGenerator;
import org.light.utils.InterVarUtil;
import org.light.utils.StringUtil;

public class DomainFieldController extends Controller implements Comparable<Controller>{
	private static final long serialVersionUID = 229666513964270905L;
	protected DomainFieldVerb dfverb;
	
    @Override
	public String generateControllerString()  throws Exception{
		if (this.dfverb.generateControllerMethod() == null) return null;
		StringBuilder sb = new StringBuilder();
		if (this.dfverb.getDomain().getPackageToken() !=null && !"".equalsIgnoreCase(this.dfverb.getDomain().getPackageToken())) sb.append("package ").append(this.dfverb.getDomain().getPackageToken()).append(".").append(this.dfverb.getDomain().getControllerSuffix()).append(";\n\n");
		Set<String> imports = this.generateImportStrings();
		for (String s: imports){
			sb.append("import ").append(s).append(";\n");
		}
		if (this.dfverb.isBelongsToUserDomain()) {
			sb.append("@WebServlet(name = \""+StringUtil.lowerFirst(this.getStandardName())+"\",urlPatterns = {\"/"+ this.dfverb.getDomain().getControllerPackagePrefix()+ this.dfverb.getDomain().getLowerFirstDomainName()+this.dfverb.getDomain().getControllerNamingSuffix()+"/"+StringUtil.lowerFirst(this.dfverb.getVerbName())+"\",\"/"+ this.dfverb.getDomain().getControllerPackagePrefix()+ "profile"+this.dfverb.getDomain().getControllerNamingSuffix()+"/"+StringUtil.lowerFirst(this.dfverb.getVerbName())+"\",\"/login"+this.dfverb.getDomain().getControllerNamingSuffix()+"/"+StringUtil.lowerFirst(this.dfverb.getVerbName())+"\"})\n");
		} else {
			sb.append("@WebServlet(name = \""+StringUtil.lowerFirst(this.getStandardName())+"\",urlPatterns = \"/"+ this.dfverb.getDomain().getControllerPackagePrefix()+ this.dfverb.getDomain().getLowerFirstDomainName()+this.dfverb.getDomain().getControllerNamingSuffix()+"/"+StringUtil.lowerFirst(this.dfverb.getVerbName())+"\")\n");
		}
		if (this.dfverb instanceof AddUploadDomainField){
            sb.append("@MultipartConfig(fileSizeThreshold = 1024*1024*10, maxFileSize=1024*1024*10, maxRequestSize=1024*1024*20)\n");
        }
		sb.append("public class ").append(this.getStandardName()).append(" extends HttpServlet{\n");		
		
		//generate fields notions
		Iterator it = this.getFields().iterator();
		  while (it.hasNext()) {	
		        Field field = (Field) it.next();
		        String fieldName = field.getFieldName();
		        String fieldType = field.getFieldType();
		        sb.append("\tprotected ").append(fieldType).append(" ").append(fieldName).append(";\n");
	        }
		  sb.append("\n");
		  //generate getter setter notions
		  it = this.getFields().iterator();
		  while (it.hasNext()) {	
		        Field field = (Field) it.next();
		        String fieldName = field.getFieldName();
		        String fieldType = field.getFieldType();
		        // generate setters
		        sb.append("\tpublic void set").append(StringUtil.capFirst(fieldName)).append("(").append(fieldType).append(" ").append(StringUtil.lowerFirst(fieldName)).append("){\n");
		        sb.append("\t\tthis.").append(StringUtil.lowerFirst(fieldName)).append(" = ").append(StringUtil.lowerFirst(fieldName)).append(";\n");
		        sb.append("\t}\n\n");
		        // generate getters
		        sb.append("\tpublic ").append(fieldType).append(" get").append(StringUtil.capFirst(fieldName)).append("(){;\n");
		        sb.append("\t\treturn this.").append(StringUtil.lowerFirst(fieldName)).append(";\n");
		        sb.append("\t}\n\n");
	        }
		  
		  Iterator it2 = this.getMethods().iterator();
		  while (it2.hasNext()){
			  sb.append(((Method)it2.next()).generateMethodString()).append("\n");
		  }
		  sb.append("}\n");
		System.out.println(sb.toString());
		return sb.toString();
	}

	public DomainFieldController(DomainFieldVerb  dfverb) throws Exception{
		super();
		this.dfverb = dfverb;
		this.standardName = dfverb.getVerbName() + dfverb.getDomain().getControllerNamingSuffix();
	    
        try {
            Method processRequest = this.dfverb.generateControllerMethod();
            Method doGet = NamedUtilMethodGenerator.generateServletDoGetCallProcessRequestMethod(2, InterVarUtil.Servlet.request, InterVarUtil.Servlet.response);
            Method doPost =  NamedUtilMethodGenerator.generateServletDoPostCallProcessRequestMethod(2, InterVarUtil.Servlet.request, InterVarUtil.Servlet.response);
            Method servletInfo = NamedUtilMethodGenerator.generateGetServletInfoMethod(2, "Powered by Mind Rules.");
            this.addMethod(processRequest);
            this.addMethod(servletInfo);
            this.addMethod(doGet);
            this.addMethod(doPost);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

	@Override
	public int compareTo(Controller o) {
        if (! (o instanceof DomainFieldController)) {
            return -1;
        }else {
            String myName = this.getStandardName();
            String otherName = o.getStandardName();
            return myName.compareTo(otherName);
        }
	}
}
